﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class BasicBase_Duty_DutyAddType : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
            Bind2();
        }

    }
    private void Bind()
    {
        RadioList.DataSource = Yipusoft.Common.BLL.CodeItem.GetList(" Left(ItemID,4)='ZBLB'");
        RadioList.DataTextField = "ItemName";
        RadioList.DataValueField = "ItemID";
        RadioList.DataBind();
        RadioList.Items[0].Selected = true;
    }
    private void Bind2()
    {
        string DeptID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        if (DeptID == null || DeptID.Length < 5)
            return;

        string strSql = string.Empty;
        if (DeptID.Trim().Substring(5, 1).Equals("0"))
            strSql = " ItemID='" + RadioList.SelectedValue + "'And Left(DepartmentID,5)='" + DeptID.Substring(0, 5) + "'";
        else
            strSql = " ItemID='" + RadioList.SelectedValue + "'And DepartmentID='" + DeptID + "'";    
        
        GvwType.DataSource = Yipusoft.Duty.BLL.DutyType.GetList(strSql);
        GvwType.DataBind();
    }
    protected void RadioList_SelectedIndexChanged(object sender, EventArgs e)
    {
        Bind2();
        PanelEdit.Visible = false;

    }

    protected void GvwType_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
             Label lblID = e.Row.FindControl("lblID") as Label;
             lblID.Text = (e.Row.RowIndex+1).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    }
    protected void IbtnEdit_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandArgument == null)
            return;
        PanelEdit.Visible = true;
        Yipusoft.Duty.Model.DutyTypeInfo info = Yipusoft.Duty.BLL.DutyType.GetModel(int.Parse(e.CommandArgument.ToString()));
        HtypeID.Value = info.ID.ToString();
        txtName.Text = info.DTName;
        txtNum.Text = info.Num.ToString();
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (HtypeID.Value.Trim() == "")
            return;
        try
        {
            Yipusoft.Duty.Model.DutyTypeInfo info = Yipusoft.Duty.BLL.DutyType.GetModel(int.Parse(HtypeID.Value));           
            info.Num =  int.Parse(txtNum.Text.Trim());
            info.DTName = txtName.Text.Trim();
            int result = Yipusoft.Duty.BLL.DutyType.Update(info);

            if (result > 0)
            {
                lblState.Text = "更新成功";
                Bind2();
            }
            else
                lblState.Text = "更新失败";
        }
        catch (Exception es) { lblState.Text = es.Message; }
        txtName.Text = "";
        txtNum.Text = "";
        HtypeID.Value = "";

    }
    protected void IbtnDelete_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandArgument == null)
            return;
        Yipusoft.Duty.BLL.DutyType.Delete(int.Parse(e.CommandArgument.ToString()));
        Bind2();
    }
 
}
